/*
 * Copyright (c) 2018. paascloud.net All Rights Reserved.
 * 项目名称：paascloud快速搭建企业级分布式微服务平台
 * 类名称：UacWebMvcConfig.java
 * 创建人：刘兆明
 * 联系方式：paascloud.net@gmail.com
 * 开源地址: https://github.com/paascloud
 * 博客地址: http://blog.paascloud.net
 * 项目官网: http://paascloud.net
 */

package com.csun.cmny.provider.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.csun.cmny.core.config.PcObjectMapper;
import com.csun.cmny.core.config.SwaggerConfiguration;
import com.csun.cmny.core.interceptor.TokenInterceptor;
import com.csun.cmny.security.core.properties.SecurityConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * The class Web mvc config.
 *
 * @author paascloud.net @gmail.com
 */
@Configuration
@EnableWebMvc
@Import(SwaggerConfiguration.class)
@Slf4j
public class UacWebMvcConfig implements WebMvcConfigurer {

	@Resource
	private TokenInterceptor vueViewInterceptor;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/");
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
//		super.addInterceptors(registry);
		log.info("vueViewInterceptor add......");
		registry.addInterceptor(vueViewInterceptor)
				.addPathPatterns("/**")
				.excludePathPatterns("/swagger-resources/**", "*.js", "/**/*.js", "*.css", "/**/*.css", "*.html", "/**/*.html", SecurityConstants.DEFAULT_SOCIAL_USER_INFO_URL);
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

//		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//		converter.setFeatures(SerializerFeature.WriteNullListAsEmpty,
//				SerializerFeature.WriteMapNullValue,
//				SerializerFeature.WriteNullNumberAsZero,
//				SerializerFeature.WriteNullStringAsEmpty,	//字符串null返回空字符串
//				SerializerFeature.WriteNullBooleanAsFalse,
//				SerializerFeature.PrettyFormat);
//		converters.add(converter);

		PcObjectMapper.buidMvcMessageConverter(converters);
	}

}
